package com.joesem.sale.client.logic.mine

import android.os.Bundle
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.igexin.sdk.PushManager
import com.jiaxing.sale.client.R
import com.joesem.sale.client.base.BaseFragment
import com.jiaxing.sale.client.databinding.FragmentMineBinding
import com.joesem.sale.client.base.createViewModel
import com.joesem.sale.client.base.launch
import com.joesem.sale.client.db.LoginTable
import com.joesem.sale.client.logic.home.MainActivity
import com.joesem.sale.client.logic.home.MainViewModel
import com.joesem.sale.client.logic.login.LoginActivity
import com.joesem.sale.client.utils.click
import com.joesem.sale.client.widget.dialog.optionConfirm


class MineFragment : BaseFragment<FragmentMineBinding>() {
    private val viewModel by createViewModel<MainViewModel>()
    override fun FragmentMineBinding.initView(savedInstanceState: Bundle?) {
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                viewModel.orderStatsNum()
            }

            override fun onCreate(owner: LifecycleOwner) {
                viewModel.accountInfo()

            }
        })
    }

    override fun FragmentMineBinding.initListener() {
        btnOrder1.click {
            (requireActivity() as MainActivity).orderChange(0)
        }
        btnOrder2.click {
            (requireActivity() as MainActivity).orderChange(1)
        }
        btnOrder3.click {
            (requireActivity() as MainActivity).orderChange(2)
        }
        btnOrder4.click {
            (requireActivity() as MainActivity).orderChange(3)
        }

        btnPersonal.click {
            launch<PersonalActivity>()
        }

        btnBackPassword.click {
        }

        btnAbout.click {
            launch<AboutMineActivity>()
        }
        btnLogout.click {
            optionConfirm(requireContext(), msg = "确定要退出登录吗？") {
                viewModel.logout()
            }
        }
    }

    override fun FragmentMineBinding.initIntent(intent: Any?) {
        when (intent) {
            is MainViewModel.AccountIntent -> {
                tvName.text = intent.data.realname.orEmpty()
                tvDeptName.text = intent.data.dept_name.orEmpty()
                tvMobile.text = intent.data.mobile.orEmpty()
                Glide.with(requireActivity())
                    .applyDefaultRequestOptions(RequestOptions().optionalCircleCrop())
                    .load(intent.data.avatar)
                    .into(ivPhoto)

            }
            is MainViewModel.OrderStatusNumCountIntent -> {
                tvOrderService.text = intent.num.服务中?.toString() ?: "0"
                tvOrderClose.text = intent.num.已关闭?.toString() ?: "0"
                tvOrderWait.text = intent.num.待处理?.toString() ?: "0"
                tvOrderCount.text = intent.num.总服务?.toString() ?: "0"
            }

            is MainViewModel.LogoutIntent -> {
                launch<LoginActivity>()
                requireActivity().finish()
            }
        }
    }
}